import pytest
import requests

from api import log
from api.api_recharge import ApiRecharge
from api.api_register_login import ApiRegisterLogin
from tools import common_assert, html_parse


class TestRecharge:
    #初始化
    @pytest.fixture(autouse=True)
    def setup(self):
         #获取session
         self.session =requests.session()
         #获取ApiRecharge 对象
         self.api=ApiRecharge(self.session)
        #  获取登录对象并进行登录
         r = ApiRegisterLogin(self.session).api_login()

    #结束
    def teardown(self):
        self.session.close()
    #充值接口测试
    def test01_img_code(self,random =123123,expect_code=200):
        try:
            result = self.api.api_img_code(random)
            print(result.status_code)
            log.info("充值获取图片验证码接口的执行结果是{}".format(result.status_code))
            common_assert(result,status_code=expect_code)
        except Exception as e:
            log.error(e)
            raise

    #后台充值接口测试
    def test02_recharge(self,expect_text="OK"):
        try:
            #获取充值图片验证码
            self.api.api_img_code(123123)
            result = self.api.api_recharge()
        # 第三方充值
            data = html_parse(result)
            result =self.session.post(url =data[0],data=data[1])
            log.info("第三方充值接口的执行结果是{}".format(result.text))
            common_assert(result,text=expect_text)
            log.info("三方充值断言通过")
        except Exception as e:
            log.error(e)
            raise